#include "myXml.h"

CXml::CXml()
{
}

CXml::~CXml()
{
}

int CXml::GetDataNum(LPCTSTR file)
{
	CMarkup xml;

	int i = 0;
	
	if(xml.Load(file))
	{
		while(xml.FindChildElem(_T("date"))) i++;  
	}

	return i;
}

bool CXml::LoadXML(LPCTSTR file, int num , xmlData date[])
{
	return LoadXML(file, num ,  1, date);
}

bool CXml::LoadXML(LPCTSTR file, int num , int time, xmlData date[])
{
	CMarkup xml;
	
	if(xml.Load(file))
	{
		for(int j=0; j<time; j++)
		{
			if(xml.FindChildElem(_T("date")))
			{
				xml.IntoElem(); 
				
				for(int i=0; i<num; i++)
				{
					if(xml.FindChildElem(date[i + j*num].szName))
						date[i + j*num].szValue = xml.GetChildData();
					else
						return false;
				}
				xml.OutOfElem();
			}
			else
				return false;
		}
		return true;
	}
	
	return false;
}

bool CXml::SetXML(LPCTSTR file, int num , xmlData date[])
{
	return SetXML(file, num,  1, date);
}

bool CXml::SetXML(LPCTSTR file, int num , int time, xmlData date[])
{
	CMarkup xml;
	
	if(xml.Load(file))
	{
		for(int j=0; j<time; j++)
		{
			if(xml.FindChildElem(_T("date")))
			{
				xml.IntoElem(); 
				for(int i=0; i<num; i++)
				{
					if(xml.FindChildElem(date[i + j*num].szName))
						xml.SetChildData(date[i + j*num].szValue);
					else
						return false;
				}
				xml.OutOfElem();
			}
			else
				return false;
		}

		xml.Save(file);
	}
	
	return true;
}

void CXml::SaveXML(LPCTSTR file)
{
	if(FindFile(file))
	{
		RemoveXML(file);
	}

	CMarkup xml;
	xml.SetDoc(_T("<?xml version=\"1.0\" encoding=\"UTF-8\"?>"));
	xml.AddElem(_T("dates"));

	xml.Save(file); 
}

void CXml::SaveXML(LPCTSTR file, int num , xmlData date[])
{
	SaveXML(file, num, 1, date);
}

void CXml::SaveXML(LPCTSTR file, int num , int time, xmlData date[])
{
	if(FindFile(file))
	{
		RemoveXML(file);
	}

	CMarkup xml;
	xml.SetDoc(_T("<?xml version=\"1.0\" encoding=\"UTF-8\"?>"));
	xml.AddElem(_T("dates"));
	xml.IntoElem();  
		
	for(int j=0; j<time; j++)
	{
		xml.AddElem(_T("date"));
		xml.IntoElem();	

		for(int i=0; i<num; i++)
		{
			xml.AddElem(date[i+j*num].szName, date[i+j*num].szValue);
		}
			
		xml.OutOfElem(); 
	}
		
	xml.OutOfElem(); 
	xml.Save(file); 
}

bool CXml::DeleteXML(LPCTSTR file, xmlData data)
{
	CMarkup xml;

	int flag = 0;

	if(xml.Load(file))
	{
		while(xml.FindChildElem(_T("date")))
		{
			xml.IntoElem(); 
			
			if(xml.FindChildElem(data.szName))
			{
				if(data.szValue == xml.GetChildData())
				{
					flag = 1;
				}
			}

			xml.OutOfElem();

			if(flag)
			{
				xml.RemoveChildElem();
				xml.Save(file);
				return true;
			}
		}
	}
	
	return false;
}

bool CXml::FindXML(LPCTSTR file, xmlData data)
{
	CMarkup xml;
	
	if(xml.Load(file))
	{
		while(xml.FindChildElem(_T("date")))
		{
			xml.IntoElem(); 
				
			if(xml.FindChildElem(data.szName))
			{
				if(data.szValue == xml.GetChildData())
				{
					return true;
				}
			}
				
			xml.OutOfElem();
		}

		xml.AddChildElem(_T("date"));
		xml.IntoElem();	

		xml.AddChildElem(data.szName, data.szValue);
			
		xml.OutOfElem(); 

		xml.Save(file); 
	}
	
	return false;
}


bool CXml::FindFile(LPCTSTR file)
{
	CFileFind finder;
	bool bWorking = finder.FindFile(file);  
	finder.Close();  
	return bWorking;
}

int CXml::FindFiles(LPCTSTR filePath, int flag, CString data[])
{
	int i=0;
	CFileFind finder;
	BOOL bWorking = finder.FindFile(filePath);   
    while( bWorking )   
    {   
        bWorking = finder.FindNextFile();   
			
        if(!finder.IsDirectory())   
        {   
			data[i++]=finder.GetFileTitle();
        }   
    }   
    
    finder.Close();  
	
	return i;
}

void CXml::RemoveXML(LPCTSTR file)
{	
	try
	{
	   CFile::Remove(file);
	}
	catch (CFileException* pEx)
	{
	   TRACE(_T("File %20s cannot be removed\n"), file);
	   pEx->Delete();
	}
}

